//
//  EndScene.cpp
//  HelloCpp
//
//  Created by apple  on 13-6-16.
//
//

#include "EndScene.h"

#include "SimpleAudioEngine.h"
#include "CC2DFileCache.h"
#include "Misc.h"
#include "GlobalDef.h"
#include "SelectScene.h"

using namespace CocosDenshion;

EndScene::~EndScene()
{
    CC_SAFE_DELETE(m_pb2World);
}

EndScene* EndScene::create()
{
    EndScene* pScene = new EndScene;
    if (pScene && pScene->init()) {
        pScene->autorelease();
        return pScene;
    }
    return NULL;
}

void EndScene::onEnter()
{
    CCScene::onEnter();
    
    scheduleUpdate();
}

void EndScene::onExit()
{
    unscheduleUpdate();
    
    CCScene::onExit();
}

#define START_MENU_ITEM 1

bool EndScene::init()
{
    bool bRet = CCScene::init();
    
	CC2DNode* pCC2DNode = CC2DFileCache::sharedCC2DFileCache()->addCC2DNode("end.cc2d");
    CCNode* endLayer = pCC2DNode->createCCNode(this);
    
	this->m_pb2World = (b2World*)endLayer->getUserData();
    
    CCMenuItem* backBtn = dynamic_cast<CCMenuItem*>(Misc::getChildByName(endLayer, "btnBack"));
    backBtn->setTag(START_MENU_ITEM);
    backBtn->setTarget(this, SEL_MenuHandler(&EndScene::menuItemClick)); 
    
    endLayer->setPosition(0, 0);
    return bRet;
}

void EndScene::menuItemClick(cocos2d::CCObject *sender)
{
    CCNode* pItem = dynamic_cast<CCNode*>(sender);
    if (pItem->getTag() == START_MENU_ITEM) {
        SelectScene* pScene = SelectScene::create();
        CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1, pScene));
    }
}

void EndScene::update(float dt)
{
    int velocityIterations = 8;
    int positionIterations = 1;
    
    std::vector<CCNode*> pDelNodes;
    if (m_pb2World) {
        m_pb2World->Step(dt, velocityIterations, positionIterations);
        //Iterate over the bodies in the physics world
        b2Body* pBody = m_pb2World->GetBodyList();
        while (pBody) {
            void* pUserData = pBody->GetUserData();
            if (pUserData) {
                //Synchronize the AtlasSprites position and rotation with the corresponding body
                CCMenuItemImage* pNode = (CCMenuItemImage*)pUserData;
                pNode->setPosition(pBody->GetPosition().x * PTM_RATIO, pBody->GetPosition().y*PTM_RATIO);
                pNode->setRotation( -1 * CC_RADIANS_TO_DEGREES(pBody->GetAngle()) );
                
                CCRect menuRect = Misc::getGlobalRect(this);
                CCRect itemRect = Misc::getGlobalRect(pNode);
                if (!menuRect.intersectsRect(itemRect)) {
                    if (itemRect.origin.y <= menuRect.origin.y) {
                        pDelNodes.push_back(pNode);
                    }
                }
            }
            pBody = pBody->GetNext();
        }
    }
    
    for (int i=0; i<pDelNodes.size(); i++) {
        b2Body* pBody = (b2Body*)pDelNodes[i]->getUserData();
        if (pBody) {
            m_pb2World->DestroyBody(pBody);
        }
        pDelNodes[i]->removeAllChildrenWithCleanup(true);
    }
}